//
//  CHSVoiceHUD.m
//  ChildHoodStemp
//
//  Created by 日东 罗 on 13-11-8.
//
//

#import "CHSVoiceHUD.h"
#import "UIUtil.h"
#import "MicroDef.h"
#import <QuartzCore/QuartzCore.h>
#import "ISTThemeLabel.h"

#pragma mark - <DEFINES>

#define _DEVICE_WINDOW ((UIView*)[UIApplication sharedApplication].keyWindow)

#pragma mark - <CLASS> - UIVewFrame

@interface UIView (UIViewFrame)

@end

@implementation UIView (UIViewFrame)


@end

#pragma mark - <CLASS> - CHSVoiceHUD
@interface CHSVoiceHUD ()
{
    ISTThemeLabel                     *_text;
    UIButton                 *_centerImageView;
    UIActivityIndicatorView     *_statusView;
    NSInteger                   _countDownTime;
    NSTimer                     *_timer;
}
@end

@implementation CHSVoiceHUD

-(void) dealloc
{
//    NSLog(@"CHSVoiceHUD dealloc");
}

-(id) init
{
    self = [super initWithFrame:CGRectMake(0, 0, 180, 180)];
    
    if (self) {
        self.center = CGPointMake(HARDWARE_SCREEN_WIDTH/2, HARDWARE_SCREEN_HEIGHT/2);
        self.backgroundColor = [UIUtil colorWithHexString:@"#000000" withAlpha:0.5f];
        self.layer.cornerRadius = 6.0f;
        self.clipsToBounds = YES;
        self.progress = 0.f;
        self.alpha = 0;
        [self createMainUI];
    }
    return self;
}

-(void) createMainUI
{
    _centerImageView = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 104, 144)];
    _centerImageView.center = CGPointMake(self.width/2, self.height/2-5);
    _centerImageView.backgroundColor = [UIColor clearColor];
    _centerImageView.userInteractionEnabled = NO;
    _centerImageView.hidden = YES;
    _centerImageView.titleLabel.font = [UIFont boldSystemFontOfSize:80.0f];
    
    _text = [[ISTThemeLabel alloc] initWithFrame:CGRectMake(0, 0, 110, 15)];
    _text.center = CGPointMake(self.width/2, self.height-10);
    _text.textAlignment = NSTextAlignmentCenter;
    _text.adjustsFontSizeToFitWidth = YES;
    _text.textColor = [UIColor whiteColor];
    [_text setBackgroundColor:RGBCOLORWITH16COLOR(0xfd8584)];
    _text.layer.cornerRadius = 6.0f;
    _text.clipsToBounds = YES;
    _text.hidden = YES;
    
    [self addSubview:_centerImageView];
    [self addSubview:_text];
}

-(void) showWithStatus:(CHSVoiceHUDStatus)status
{
    [UIApplication sharedApplication].keyWindow.userInteractionEnabled = NO;
    [_DEVICE_WINDOW addSubview:self];
    switch (status) {
        case CHSVoiceHUDStatusShortTime:
        {
            [_centerImageView setImage:[UIImage imageNamed:@"record_shorttime.png"] forState:UIControlStateNormal];
            [_centerImageView setTitle:@"" forState:UIControlStateNormal];
            _text.text = @"说话时间太短";
            _text.backgroundColor = [UIColor clearColor];
            break;
        }
        case CHSVoiceHUDStatusPrepare:
        {
            self.alpha = 1.0;
            _statusView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
            _statusView.frame = CGRectMake(0, 0, 20, 20);
            [self addSubview:_statusView];
             [_centerImageView setImage:[UIImage imageNamed:@"record_prepare.png"] forState:UIControlStateNormal];
            [_centerImageView setTitle:@"" forState:UIControlStateNormal];
            _centerImageView.hidden = NO;
            _text.text = @"正在初始化...";
            _text.backgroundColor = [UIColor clearColor];
            _text.hidden = NO;
            break;
        }
        case CHSVoiceHUDStatusRecording:
        {
            if (_statusView) {
                [_statusView removeFromSuperview];
                _statusView = nil;
            }
            [_centerImageView setImage:[UIImage imageNamed:@"record_animate_0.png"] forState:UIControlStateNormal];
            [_centerImageView setTitle:@"" forState:UIControlStateNormal];
            _text.text = @"手指上滑，取消发送";
            _text.backgroundColor = [UIColor clearColor];
            break;
        }
            
        case CHSVoiceHUDStatusCountDown:
        {
//            _countDownTime = 10;
//            _timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateText) userInfo:nil repeats:YES];
//            [_centerImageView setTitle:[NSString stringWithFormat:@"%zd",_countDownTime] forState:UIControlStateNormal];
//            [_centerImageView setImage:nil forState:UIControlStateNormal];
//            _text.text = @"手指上滑，取消发送";
//            [[NSRunLoop mainRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
            break;
        }
        case CHSVoiceHUDStatusCancel:
        {
            [_centerImageView setImage:[UIImage imageNamed:@"record_state_cancel.png"] forState:UIControlStateNormal];
            [_centerImageView setTitle:@"" forState:UIControlStateNormal];
            _text.text = @"松开手指，取消发送";
            [_text setBackgroundColor:RGBCOLORWITH16COLOR(0xfd8584)];
            break;
        }
        default:
            break;
    }
}

-(void) hide
{
    [UIApplication sharedApplication].keyWindow.userInteractionEnabled = YES;
    [_timer invalidate];
    _timer = nil;
    [self removeFromSuperview];
}

- (void)setProgress:(float)progress
{
    //最大50  0
    //图片 小-》大
    NSString *imgName = nil;
    if (0<progress<=0.05) {
        imgName = @"record_animate_0.png";
    }else if (0.05<progress<=0.25) {
        imgName = @"record_animate_1.png";
    }else if (0.25<progress<=0.375) {
        imgName = @"record_animate_2.png";
    }else if (0.375<progress<=0.50) {
        imgName = @"record_animate_3.png";
    }else if (0.50<progress<=0.625) {
        imgName = @"record_animate_4.png";
    }else if (0.625<progress<=0.75) {
        imgName = @"record_animate_5.png";
    }else if (0.75<progress<=0.875) {
        imgName = @"record_animate_6.png";
    }else {
        imgName = @"record_animate_7.png";
    }
    [_centerImageView setImage:[UIImage imageNamed:imgName] forState:UIControlStateNormal];
    [_centerImageView setTitle:@"" forState:UIControlStateNormal];
}

- (void)setCountDownTime:(NSInteger)countDownTime
{
    if (countDownTime == 0) {
//        [_centerImageView setImage:[UIImage imageNamed:@"record_shorttime.png"] forState:UIControlStateNormal];
        [_centerImageView setTitle:@"0" forState:UIControlStateNormal];
       _text.text = @"发送语音时间过长";
    } else {
        [_centerImageView setImage:nil forState:UIControlStateNormal];
        [_centerImageView setTitle:[NSString stringWithFormat:@"%zd",countDownTime] forState:UIControlStateNormal];
         _text.text = @"手指上滑，取消发送";
    }
    
}

-(void) removeFromSuperview
{
    [UIApplication sharedApplication].keyWindow.userInteractionEnabled = YES;
    [super removeFromSuperview];
}

@end
